﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;
using System.Xml;


namespace Bainx.Extend
{
    /// <summary>
    /// 信息处理
    /// </summary>
    public static class Information
    {
        /// <summary>
        /// 异常信息文件
        /// </summary>
        public static string ExceptionFile = "Error.log";
        /// <summary>
        /// 是否输出调式信息
        /// </summary>
        public static bool IsDebug = true;
        /// <summary>
        /// 显示异常信息,不递归内部异常
        /// </summary>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        public static System.Windows.MessageBoxResult Show(this Exception exception)
        {
            return Show(exception, false);
        }
        /// <summary>
        /// 显示异常信息
        /// </summary>
        /// <param name="exception">异常</param>
        /// <param name="recurrence">是否递归</param>
        public static System.Windows.MessageBoxResult Show(this Exception exception, bool recurrence)
        {
            if (recurrence)
                return MessageBox.Show(Message(exception));
            else
                return MessageBox.Show(exception.Message);
        }
        /// <summary>
        /// 递归异常信息
        /// </summary>
        /// <param name="exception">异常</param>
        public static string Message(this Exception exception)
        {
            string str = "";
            Exception ex = exception;
            while (ex != null)
            {
                str += ex.Message + Environment.NewLine;
                ex = ex.InnerException;
            }
            return str;
        }
        /// <summary>
        /// 调式信息,只在IsDeBug为true时输出详细信息
        /// </summary>
        public static string DebugMessage(this Exception ex)
        {
            string ret = "";
            if (ex != null)
            {
                ret = "<Exception>" + Environment.NewLine
                                   + "<DateTime>" + Environment.NewLine
                                        + DateTime.Now.ToString() + Environment.NewLine
                                   + "</DateTime>" + Environment.NewLine
                                   + "<Type>" + Environment.NewLine
                                        + ex.GetType().ToString() + Environment.NewLine
                                   + "</Type>" + Environment.NewLine;
                if (IsDebug)
                {
                    ret = "<Message>" + Environment.NewLine
                                + ex.Message + Environment.NewLine
                          + "</Message>" + Environment.NewLine
                          + "<Source>" + Environment.NewLine
                                + ex.Source + Environment.NewLine
                          + "</Source>" + Environment.NewLine
                          + "<TargetSite>" + Environment.NewLine
                                + ex.TargetSite.ToString() + Environment.NewLine
                          + "</TargetSite>" + Environment.NewLine
                          + "<StackTeace>" + Environment.NewLine
                                + ex.StackTrace + Environment.NewLine
                         + "</StackTeace>" + Environment.NewLine
                         + "<InnerException>" + Environment.NewLine
                                + ex.InnerException.DebugMessage()
                         + "</InnerException>" + Environment.NewLine;
                }
                ret += "</Exception>" + Environment.NewLine;
            }
            return ret;
        }       
        /// <summary>
        /// 将信息文本写入指定的文件
        /// </summary>
        /// <param name="FilePathName"></param>
        /// <param name="outObject"></param>
        public static void MessageToFile(string FilePathName, object outObject)
        {
            try
            {
                File.AppendAllText(FilePathName, outObject.ToString());
            }
            catch(Exception ex)
            {
                Show(ex, false);
            }
        }
        /// <summary>
        /// 把异常信息输出到文本。
        /// </summary>
        /// <param name="outText">输出文本</param>
        public static void MessageToLogFile(string outText)
        {
            MessageToFile(ExceptionFile,outText);
        }
        /// <summary>
        /// 将异常写入Error.log文件
        /// </summary>
        /// <param name="ex"></param>
        public static void MessageToLogFile(this Exception ex)
        {            
            MessageToLogFile(ex.DebugMessage());            
        }

        

    }
}
